/**
 * 深拷贝对象或数组
 * @param {*要拷贝的Object或者Array} obj
 */
export function cloneObj(obj) {
  let newobj
  if (typeof obj !== 'object') {
    return obj
  } else if (obj && typeof obj === 'object') {
    if (obj.constructor === Array) {
      newobj = []
    } else if (obj.constructor === Object) {
      newobj = {}
    } else {
      return obj
    }
    for (let key in obj) {
      if (Object.hasOwnProperty.call(obj, key)) {
        // 判断ojb子元素是否为对象，如果是，递归复制
        if (obj[key] && typeof obj[key] === 'object') {
          newobj[key] = cloneObj(obj[key])
        } else {
          // 如果不是，简单复制
          newobj[key] = obj[key]
        }
      }
    }
  }
  return newobj
}

export function isEmpty(val, defaultValue) {
  let isValuetemp = val === '' || val === undefined || val === null
  if (defaultValue !== undefined) {
    if (isValuetemp) {
      return defaultValue
    }
    return val
  } else {
    return isValuetemp
  }
}

/**
 * 判断数据类型
 * @param {*} data
 * @returns
 */
export function getDataType(data) {
  let d = Object.prototype.toString.call(data)
  if (d === '[object String]') {
    return 'string'
  } else if (d === '[object Array]') {
    return 'array'
  } else if (d === '[object Object]') {
    return 'object'
  } else if (d === '[object RegExp]') {
    return 'regexp'
  } else if (d === '[object Function]') {
    return 'function'
  }
}

/**
 * 深度 merge
 * @param {*} target
 * @param  {...any} arg
 * @returns
 */
export function deepMerge(target, ...arg) {
  return arg.reduce((acc, cur) => {
    return Object.keys(cur).reduce((subAcc, key) => {
      const srcVal = cur[key]
      if (getDataType(srcVal) === 'object') {
        subAcc[key] = deepMerge(subAcc[key] ? subAcc[key] : {}, srcVal)
      } else if (Array.isArray(srcVal)) {
        // series: []，下层数组直接赋值
        subAcc[key] = srcVal.map((item, idx) => {
          if (getDataType(item) === 'object') {
            const curAccVal = subAcc[key] ? subAcc[key] : []
            return deepMerge(curAccVal[idx] ? curAccVal[idx] : {}, item)
          } else {
            return item
          }
        })
      } else {
        subAcc[key] = srcVal
      }
      return subAcc
    }, acc)
  }, target)
}

/**
 * 删除对象空节点
 * 会改变原数组
 * @param {*} obj
 */
export function deleteEmptyProperty(obj, toDel = [null, undefined, '']) {
  for (let key in obj) {
    let item = obj[key]
    if (getDataType(item) === 'array') {
      item.forEach(v => {
        if (getDataType(item[v]) === 'object' || getDataType(item[v]) === 'array') {
          deleteEmptyProperty(item[v])
        }
      })
    } else if (getDataType(item) === 'object') {
      deleteEmptyProperty(item)
    } else {
      let isEmpty = toDel.some(v => {
        if (Number.isNaN(v)) {
          return Number.isNaN(item)
        }
        return v === item
      })
      if (isEmpty) {
        delete obj[key]
      }
    }
  }
}

export function changeProtocol(url) {
  if (!url) return
  var res = url
  if (url.indexOf('http:') !== -1) {
    if (global.location.protocol === 'https:') {
      res = url.replace('http:', 'https:')
    }
  } else if (url.indexOf('https:') === -1) {
    res = global.location.protocol + '//' + url
  }
  return res
}

export function getNextLetter(letter, index = 1) {
  return String.fromCharCode(letter.charCodeAt(0) + index)
}

// 获取时长 format
export function getDurationTime(value) {
  let hour = parseInt(value / 3600)
  if (hour < 10) {
    //时
    hour = '0' + hour
  }
  var minute = parseInt((value % 3600) / 60)
  if (minute < 10) {
    //分
    minute = '0' + minute
  }
  var second = Math.ceil(value % 60)
  if (second < 10) {
    //秒
    second = '0' + second
  }
  return [hour, minute, second]
}

export function dateGet(date) {
  return (date.getTime() - new Date(new Date().toLocaleDateString()).getTime()) / 1000
}

export const createUuid = () => {
  // I generate the UID from two parts here
  // to ensure the random number provide enough bits.
  var firstPart = (Math.random() * 46656) | 0
  var secondPart = (Math.random() * 46656) | 0
  firstPart = ('000' + firstPart.toString(36)).slice(-3)
  secondPart = ('000' + secondPart.toString(36)).slice(-3)
  return firstPart + secondPart
}

export const getQueryStringRegExp = name => {
  var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i')
  if (reg.test(location.href)) return decodeURIComponent(RegExp.$2.replace(/\+/g, ' '))
  return ''
}
